package com.sky.config;

import cn.hutool.core.text.AntPathMatcher;
import com.sky.constant.AuthConstant;
import com.sky.dto.member.UmsMemberVO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Enumeration;
import java.util.Objects;


public class LoginUserInterceptor implements HandlerInterceptor {

    public static ThreadLocal<UmsMemberVO> threadLocal = new ThreadLocal<>();
    public static ThreadLocal<Long> threadLocalMemberId = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        boolean match = new AntPathMatcher().match("/api/**", requestURI);
        if (match){
            return true;
        }

        UmsMemberVO umsMemberVO = (UmsMemberVO)request.getSession().getAttribute(AuthConstant.SESSION_USER_INFO);
        if (Objects.nonNull(umsMemberVO)){
            threadLocal.set(umsMemberVO);
            return true;
        } else {
            request.setAttribute("errorMsg", "请先登录");
            response.sendRedirect("http://auth.skymall.com/login.html");
            return false;
        }
    }
}
